package com.jams.goodox.activity.serve.enterprise;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.constant.Constants;
import com.jams.goodox.entity.LogisticsBean;
import com.jams.goodox.entity.resp.LogisticsResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.EnterpriseService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DelayUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.UserUtils;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;
import com.jams.library.listener.OnRecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * Created by jams on 2017/8/7.
 * ***
 * Tel: ***
 *
 * 行政后勤
 */

public class LogisticsActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @Bind(R.id.rcv_server)
    RecyclerView rcvServer;
    @Bind(R.id.empty_view)
    LoadView emptyView;
    @Bind(R.id.srf_server_page)
    SwipeRefreshLayout srfServerPage;
    public static final String TITLE = "TITLE";
    List<LogisticsBean> serverBeanList;
    private RecycleCommonAdapter<LogisticsBean> serverAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_server_list_page;
    }

    @Override
    protected void init() {
        String title = getIntent().getStringExtra(TITLE);
        StyleUtil.titleBackKey(this, StringUtils.newString(title));
        serverBeanList = new ArrayList<>();
        rcvServer.setLayoutManager(new GridLayoutManager(getContext(), 4));
        serverAdapter = new RecycleCommonAdapter<LogisticsBean>(getContext(), serverBeanList, R.layout.server_item) {
            @Override
            public void convert(RecycleCommonViewHolder helper, final LogisticsBean item, int position) {
                helper.setSimpleDraweeView(R.id.sdv_icon_image_item, item.getSectionImg())
                        .setText(R.id.tv_icon_name_item, item.getSectionName())
                        .setOnItemClickListener(new OnRecycleItemClickListener() {
                            @Override
                            public void itemClick() {
                                if (UserUtils.checkIsCompetent()) {
                                }
                            }
                        });
            }
        };
        rcvServer.setAdapter(serverAdapter);
        srfServerPage.setOnRefreshListener(this);
        onRefresh();
    }

    private void getServerList() {
        enqueue(createCallService(EnterpriseService.class).getLogisticsList(Constants.X_Z_H_Q), new RetrofitCallbackListener<LogisticsResp>() {
            @Override
            public void onSuccess(Call<LogisticsResp> call, LogisticsResp result) {
                srfServerPage.setRefreshing(false);
                if (result != null) {
                    serverBeanList.clear();
                    serverBeanList.addAll(result.getData());
                    serverAdapter.notifyDataSetChanged();
                }
                if (!CollectionUtils.hasData(serverBeanList)) {
                    emptyView.loadCompleteNoData(R.mipmap.holder_no_common,"暂无相关内容");
                } else {
                    emptyView.loadComplete();
                }
            }

            @Override
            public void onError(String msg) {
                srfServerPage.setRefreshing(false);
                if (!CollectionUtils.hasData(serverBeanList)) {
                    emptyView.loadCompleteNoData(R.mipmap.holder_no_common,"暂无相关内容");
                } else {
                    emptyView.loadComplete();
                }
            }
        });
    }
    @Override
    public void onRefresh() {
        DelayUtils.doAfterDelay(100, new DelayUtils.DelayDoListener() {
            @Override
            public void doAction() {
                srfServerPage.setRefreshing(true);
                getServerList();
            }
        });
    }
}
